package com.ruoyi.system.service.standard.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.system.domain.EasyExcelStandardData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.standard.StandardExcelMapper;
import com.ruoyi.system.domain.standard_relevant.StandardExcel;
import com.ruoyi.system.service.standard.IStandardExcelService;

/**
 * excel(标准三级指标)Service业务层处理
 *
 * @author ruoyi
 * @date 2024-07-16
 */
@Service
public class StandardExcelServiceImpl implements IStandardExcelService
{
    @Autowired
    private StandardExcelMapper standardExcelMapper;

    /**
     * 查询excel(标准三级指标)
     *
     * @param id excel(标准三级指标)主键
     * @return excel(标准三级指标)
     */
    @Override
    public StandardExcel selectStandardExcelById(Long id)
    {
        return standardExcelMapper.selectStandardExcelById(id);
    }

    /**
     * 查询excel(标准三级指标)列表
     *
     * @param standardExcel excel(标准三级指标)
     * @return excel(标准三级指标)
     */
    @Override
    public List<StandardExcel> selectStandardExcelList(StandardExcel standardExcel)
    {
        return standardExcelMapper.selectStandardExcelList(standardExcel);
    }

    /**
     * 新增excel(标准三级指标)
     *
     * @param standardExcel excel(标准三级指标)
     * @return 结果
     */
    @Override
    public int insertStandardExcel(StandardExcel standardExcel)
    {
        return standardExcelMapper.insertStandardExcel(standardExcel);
    }



    /**
     * 批量删除excel(标准三级指标)
     *
     * @param ids 需要删除的excel(标准三级指标)主键
     * @return 结果
     */
    @Override
    public int deleteStandardExcelByIds(Long[] ids)
    {
        return standardExcelMapper.deleteStandardExcelByIds(ids);
    }

    /**
     * 删除excel(标准三级指标)信息
     *
     * @param id excel(标准三级指标)主键
     * @return 结果
     */
    @Override
    public int deleteStandardExcelById(Long id)
    {
        return standardExcelMapper.deleteStandardExcelById(id);
    }

    /**
     * new ------------------------------------
     */
    /**
     * 根据标准号查询excel(标准三级指标)
     */
    @Override
    public StandardExcel selectStandardExcelByStandardCode(String standardCode)
    {
        return standardExcelMapper.selectStandardExcelByStandardCode(standardCode);
    }
    /**
     * 修改excel(标准三级指标)
     *
     * @param standardExcel excel(标准三级指标)
     * @return 结果
     */
    @Override
    public int updateStandardExcel(StandardExcel standardExcel)
    {
        return standardExcelMapper.updateStandardExcel(standardExcel);
    }


    /**
     * 批量更新
     * @param list
     * @return
     */
    @Override
    public int batchSave(List<EasyExcelStandardData> list) {
        //遍历list，根据数据库中的standardCode字段是否和list当中的standardCode字段匹配分成插入list和更新list
        //若standardCode存在，则更新，若不存在，则插入
        List<EasyExcelStandardData> updateList = new ArrayList<>();
        List<EasyExcelStandardData> insertList = new ArrayList<>();
        if(list!=null&&list.size()!=0) {

            for (EasyExcelStandardData easyStandard : list) {
                StandardExcel standard = standardExcelMapper.selectStandardExcelByStandardCode(easyStandard.getStandardCode());
                if (standard != null && easyStandard.getStandardCode() == standard.getStandardCode()) {
                    easyStandard.setId(standard.getId());
                    updateList.add(easyStandard);
                } else {
                    insertList.add(easyStandard);
                }
            }
        }
        if(insertList != null&&insertList.size()!=0)
            standardExcelMapper.insertStandardExcelList(insertList);
        if(updateList!=null&&updateList.size()!=0)
            standardExcelMapper.updateStandardExcelList(updateList);

        return 1;
        // 如果是mybatis,尽量别直接调用多次insert,自己写一个mapper里面新增一个方法batchInsert,所有数据一次性插入
    }

    /**
     * 批量查询execl
     * @return List<StandardExcel>
     */
    @Override
    public List<StandardExcel> selectStandardExeclByStandardCodes(List<String> standardCodes) {
        return standardExcelMapper.selectStandardExcelByStandardCodes(standardCodes);
    }

    /**
     * 删除 by standardCode
     */
    @Override
    public int deleteStandardExcelByStandardCode(String standardCode) {
        return standardExcelMapper.deleteStandardExcelByStandardCode(standardCode);
    }

}
